package com.wzl.customviewproject.view;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

import com.wzl.customviewproject.util.FlingHelper;

import org.jetbrains.annotations.NotNull;

/**
 * 作者：longxiansen
 * 功能：
 */
public class MyNestedScrollview extends NestedScrollView {
    private int velocityY;//记录当前滑动的Y轴加速度
    private boolean isStartFling = false;//用于判断RecyclerView是否在fling

    @RequiresApi(api = Build.VERSION_CODES.M)
    public MyNestedScrollview(@NonNull @NotNull Context context) {
        super(context);
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void init() {
        mFlingHelper = new FlingHelper(getContext());
        setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (isStartFling){
                    totalDy = 0;
                    isStartFling = false;
                }
                if (scrollY == 0){
    
                }
                if (scrollY == (getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())){
                    dispatchChildFling();
                }
                //在RecyclerView fling情况下，记录当前RecyclerView在Y轴的偏移
                totalDy += scrollY - oldScrollY;
            }
        });
    }

    public MyNestedScrollview(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyNestedScrollview(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    int totalDy = 0;
    private ViewGroup contentView;
    private ViewGroup topView;
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //((ViewGroup)getChildAt(0)) 是NestedScrollView下的LinearLayout

        //LinearLayout下的LinearLayout包含tabLayout和recyclerview
        contentView = (ViewGroup) ((ViewGroup)getChildAt(0)).getChildAt(1);
        //LinearLayout下的recyclerview
        topView = (ViewGroup) ((ViewGroup)getChildAt(0)).getChildAt(0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //将屏幕高度设为LinearLayout的高度，解决吸顶问题
        ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
        layoutParams.height = getMeasuredHeight();
        contentView.setLayoutParams(layoutParams);
    }

    @Override
    public void onNestedPreScroll(@NonNull @NotNull View target, int dx, int dy, @NonNull @NotNull int[] consumed, int type) {
        super.onNestedPreScroll(target, dx, dy, consumed, type);

        //内部View移动的距离dx,dy
        //内部view有移动并且滑动距离小于外部列表的高度，那么由父View去滑动
        boolean hideTop = dy > 0 && getScrollY() < topView.getMeasuredHeight();
        if (hideTop) {
            scrollBy(0,dy);
            consumed[1] = dy;//表示父View要消耗这么多距离。
        }
    }

    FlingHelper mFlingHelper;
    private void dispatchChildFling(){
        if (velocityY != 0){
            double splineFlingDistance = mFlingHelper.getSplineFlingDistance(velocityY);
            if (splineFlingDistance > totalDy){
                childFling(mFlingHelper.getVelocityByDistance(splineFlingDistance - Double.valueOf(totalDy)));
            }
        }
        totalDy = 0;
        velocityY = 0;
    }

    private void childFling(int velY){
        RecyclerView childRecyclerView = getChildRecyclerView(contentView);
        if (childRecyclerView!=null){
            childRecyclerView.fling(0,velY);
        }
    }

    private RecyclerView getChildRecyclerView(ViewGroup contentView) {
        return (RecyclerView) contentView.getChildAt(1);
    }

    @Override
    public void fling(int velocityY) {
        super.fling(velocityY);
        if (velocityY <= 0) {
            this.velocityY = 0;
        }else {
            isStartFling = true;
            this.velocityY  = velocityY;
        }
    }



}
